
//
//  VistorView.swift
//  Weibo
//
//  Created by gdarkness on 16/3/20.
//  Copyright © 2016年 gdarkness. All rights reserved.
//

import UIKit
//定义协议
protocol VistorViewDelegate: NSObjectProtocol{
    func vistorViewResisterButtonClick()
    func vistorViewLoginButtonClick()
}

class VistorView: UIView {
        required init?(coder aDecoder: NSCoder) {
            //super.init(coder: aDecoder)
            fatalError("init(coder:) has not been implemented")
        }
    //定义代理属性

    weak var vistorViewDelegate : VistorViewDelegate?
    
    override init(frame: CGRect){
        super.init(frame : frame)  //只有调用 super.init之后对象才创建完毕
        
        perpareUI()
    }
    //从xib/storyboard里加载走这个方法
    //当子类实现init(frame:CGRect)就不会集成父类的构造函数init?(coder aDecoder:NSCode),就不能从xib/storyboard里面加载
    //只有同时实现init？(frame:CGRect)和init？(coder aDecoder : NSCoder)才能保证view即可以使用代码创建，也可以使用xib/storyboard来创建
//    required init?(coder aDecoder:NScoder){
//        super.init(coder : aDecoder)
//    }


    //准备UI
   private func perpareUI(){
    self.backgroundColor = UIColor(white: 237 / 255.0, alpha: 1)
        //将懒加载的控件添加到父控件里面
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints  = false
        
        //转轮CenterX
        //约束都添加到父控件上面
        /**
        item :要约束的view
        attribute:要约束的view的属性
        relatedBy:参照
        toItem:参照的view
        */
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //转轮CenterY
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        //房子
        //CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint (item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //CenterY 和转轮CenterY重合
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        //messageLabel
        //宽度240
        //注意:当toItem为nil 时，attribute必须填NSLayoutAttribute.NotAnAttribute
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 240))
        //CenterX和转轮CenterX重合
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        //top和转轮Bottom间距16
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //注册按钮
        //左边和消息的左边重合
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Leading, relatedBy:NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        //top和消息button间距16
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem:messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        //宽度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        //高度
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        //右边和消息的右边重合
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        //top和注册按钮的top对齐
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        //宽度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
        //高度
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Height, multiplier: 1, constant: 0))
        //遮盖
        //填充父控件
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: registerButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
        
        
    }
    func setupVistorView(imageName: String, message: String){
        //隐藏房子
        homeView.hidden = true
        //设置图片
        iconView.image = UIImage(named: imageName)
        //设置message
        messageLabel.text = message
        //隐藏遮盖
        //coverView.hidden = true
        //将子view移动到最上层
        //bringSubviewToFront（coverView）
    }
    func iconViewRotation(){
        //创建核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 20
        //动画完成的时候不要移除，不然切换到其它界面在切换回来就不会动画了
        anim.removedOnCompletion = false
        //将核心动画添加到view上面
        iconView.layer.addAnimation(anim, forKey: nil)
    }
    //MARK: -- 按钮点击事件
    func registerButtonClick(){
        //调用代理方法
        vistorViewDelegate?.vistorViewResisterButtonClick()
    }
    
    func LoginButtonClick(){
        //调用代理方法
        vistorViewDelegate?.vistorViewLoginButtonClick()
    }
    //MARK: - lazy login
    lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //转轮
    lazy var iconView :UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //消息label
    lazy var messageLabel :UILabel = {
        let label =  UILabel()
        label.text = "关注一些人,看看有上面惊喜"
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(18)
        label.numberOfLines = 0
        
        return label
    }()
    // 注册
    lazy var registerButton: UIButton = {
        let button = UIButton()
        //设置
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState:UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        //添加事件
        button.addTarget(self, action: "registerButtonClick", forControlEvents:UIControlEvents.TouchUpInside)
        return button
    }()
    lazy var loginButton : UIButton = {
       let button = UIButton()
        //设置
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登录", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        //添加点击事件
        button.addTarget(self, action: "LoginButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
}
